package com.ungs.form;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.ungs.bo.FormatoPapelBO;
import com.ungs.bo.impl.FormatoPapelBOImpl;
import com.ungs.entities.FormatoPapel;
import com.ungs.utils.ValidacionCampo;

public class AddFormatoPapel extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtAlto;
	private JTextField txtAncho;
	private FormatoPapelBO formatoPapelBO;
	private List<FormatoPapel> formatoPapelList;

	public AddFormatoPapel(JFrame parent) {
		super(parent, true);
		setResizable(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Agregar Formato Papel");
		initForm();
		formatoPapelBO = new FormatoPapelBOImpl();
		formatoPapelList = formatoPapelBO.getAllFormatoPapel();
	}

	private void initForm() {
		setResizable(false);
		setBounds(100, 100, 264, 192);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblAgregarFormatoDe = new JLabel("Agregar Formato de Papel");
			lblAgregarFormatoDe.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblAgregarFormatoDe.setBounds(10, 11, 197, 32);
			contentPanel.add(lblAgregarFormatoDe);
		}
		{
			JLabel lblAlto = new JLabel("Alto:");
			lblAlto.setBounds(10, 89, 46, 14);
			contentPanel.add(lblAlto);
		}
		{
			txtAlto = new JTextField();
			txtAlto.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					ValidacionCampo.verificarSoloNumeros(e);
					ValidacionCampo.verificarLargoCamposJTextField(e, txtAlto,
							4);
				}
			});
			txtAlto.setBounds(65, 86, 69, 20);
			contentPanel.add(txtAlto);
		}
		{
			txtAncho = new JTextField();
			txtAncho.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					ValidacionCampo.verificarSoloNumeros(e);
					ValidacionCampo.verificarLargoCamposJTextField(e, txtAncho,
							4);
				}
			});
			txtAncho.setBounds(66, 54, 69, 20);
			contentPanel.add(txtAncho);
		}
		{
			JLabel lblAncho = new JLabel("Ancho:");
			lblAncho.setBounds(10, 57, 46, 14);
			contentPanel.add(lblAncho);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Agregar");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						eventoBotonAceptar();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancelar");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						eventoBotonCancelar();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void eventoBotonAceptar() {
		if (!txtAlto.getText().trim().isEmpty()
				&& !txtAncho.getText().trim().isEmpty()) {
			int alto = Integer.parseInt(txtAlto.getText().trim());
			int ancho = Integer.parseInt(txtAncho.getText().trim());

			boolean existe = false;
			for (FormatoPapel formatoPapel : formatoPapelList) {
				if (formatoPapel.getAlto() == alto
						&& formatoPapel.getAncho() == ancho) {
					existe = true;
				}
			}

			if (!existe) {
				FormatoPapel formatoPapel = new FormatoPapel();
				formatoPapel.setAlto(alto);
				formatoPapel.setAncho(ancho);
				formatoPapelBO.guardar(formatoPapel);
				JOptionPane.showMessageDialog(this,
						"Se ha agregado con exito.", "",
						JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "El formato ya existe",
						"Advertencia", JOptionPane.WARNING_MESSAGE);
				txtAlto.grabFocus();
				txtAlto.selectAll();
			}
		}
	}

	private void eventoBotonCancelar() {
		this.setVisible(false);
		this.dispose();
	}

}
